home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / JIS.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  31KB  |  1,291 lines

  1. /* Copyright (C) Ken R. Lunde, 1991-1992.  All rights reserved. */
  2.  
  3. /* These routines originally came from Ken R. Lunde's JCONV.C program, */
  4. /* version 2.3, August 12, 1992.  Many thanks to Ken for his donation. */
  5.  
  6. /* The original code was reformatted (to fit the style of the rest     */
  7. /* of the program).  Because EUC is used as the internal representa-   */
  8. /* tion, code corresponding to conversions not involving EUC is        */
  9. /* deleted.  Also, the original file-based character I/O machanism     */
  10. /* is changed.                                                         */
  11.  
  12. /* A modified version of the original documentation follows...         */
  13.  
  14. /*
  15. Program: jconv.c
  16. Version: 2.3
  17. Date:    August 12, 1992
  18. Author:  Ken R. Lunde, Adobe Systems Incorporated
  19.   EMAIL: lunde@mv.us.adobe.com
  20.   MAIL : 1585 Charleston Road, P.O. Box 7900, Mountain View, CA 94039-7900
  21. Type:    A tool for converting the Japanese code of Japanese textfiles.
  22. Code:    ANSI C (portable)
  23.  
  24. PORTABILITY:
  25. This source code was written so that it would be portable on C compilers which
  26. conform to the ANSI C standard. It has been tested on a variety of compilers.
  27.  
  28. I used THINK C and GNU C as my development platforms. I left in the Macintosh-
  29. specific lines of code so that it would be easier to enhance/debug this tool
  30. later. For those of you who wish to use this tool on the Macintosh, simply
  31. add the ANSI library to the THINK C project, and then build the application.
  32. Be sure that THINK_C has been defined, though, as the conditional compilation
  33. depends on it. You then have a double-clickable application, which when
  34. launched, will greet you with a Macintosh-style interface.
  35.  
  36. DISTRIBUTION AND RESTRICTIONS ON USAGE:
  37.  1) Please give this source code away to your friends at no charge.
  38.  2) Please try to compile this source code on various platforms to check for
  39.     portablity, and please report back to me with any results be they good or
  40.     bad. Suggestions are always welcome.
  41.  3) Only use this tool on a copy of a file -- do not use an original. This
  42.     is just common sense.
  43.  4) This source code or a compiled version may be bundled with commercial
  44.     software as long as the author is notified beforehand. The author's name
  45.     should also be mentioned in the credits.
  46.  5) Feel free to use any of the algorithms for your own work. Many of them are
  47.     being used in other tools I have written.
  48.  6) The most current version can be obtained by requesting a copy directly
  49.     from me.
  50.  
  51. DESCRIPTION:
  52.  1) Supports Shift-JIS, EUC, New-JIS, Old-JIS, and NEC-JIS for both input and
  53.     output.
  54.  2) Automatically detects infile's Japanese code (the ability to force an
  55.     input Japanese code is also supported through a command-line option).
  56.  3) The ability to convert Shift-JIS and EUC half-width katakana into full-
  57.     width equivalents. Note that half-width katakana includes other symbols
  58.     such as a Japanese period, Japanese comma, center dot, etc.
  59.  4) Supports conversion between the same code (i.e., EUC -> EUC, Shift-JIS ->
  60.     Shift-JIS, etc.). This is useful as a filter for converting half-width
  61.     katakana to their full-width equivalents.
  62.  5) If the infile does not contain any Japanese, then its contents are
  63.     echoed to the outfile. It will also try to repair the file as though
  64.     it had stripped escape characters (see #6 below).
  65.  6) The functionality of my other tool called repair-jis.c is included in
  66.     this tool by using the "-r[CODE]" option. This will recover stripped
  67.     escape characters, then convert the file to be in CODE format.
  68. */
  69.  
  70. #include "jwp.h"
  71.  
  72. #include <ctype.h>
  73. #include <errno.h>
  74.  
  75.  
  76. #define TOFULLSIZE        TRUE
  77. #define UNGETBUFSIZ     10
  78.  
  79. #define ReadChar()      ((stackp > 0) ? UngetBuf[--stackp] : f_charin())
  80. #define WriteChar(x)    f_charout(x)
  81. #define UnreadChar(x)   UngetBuf[stackp++] = (x)
  82.  
  83.  
  84.  
  85. #define FF_EUCSJIS  (FF_NEC + 1)
  86.  
  87. #define NUL         0
  88. #define NL          10
  89. #define FF          12
  90. #define CR          13
  91. #define ESC         27
  92.  
  93. #define SJIS1(A)    ((A >= 129 && A <= 159) || (A >= 224 && A <= 239))
  94. #define SJIS2(A)    (A >= 64 && A <= 252)
  95. #define HANKATA(A)  (A >= 161 && A <= 223)
  96. #define ISEUC(A)    (A >= 161 && A <= 254)
  97. #define ISMARU(A)   (A >= 202 && A <= 206)
  98. #define ISNIGORI(A) ((A >= 182 && A <= 196) || (A >= 202 && A <= 206))
  99.  
  100. void StraightEcho (void);
  101. void HanToZen(int *p1, int *p2, FILEFORMAT incode);
  102. void SJisToJis(int *p1, int *p2);
  103. void JisToSJis(int *p1, int *p2);
  104. void ShiftToEuc(FILEFORMAT incode);
  105. void EucToSeven(FILEFORMAT incode, char *ki, char *ko);
  106. void EucToEuc(FILEFORMAT incode);
  107. void EucToShift(FILEFORMAT incode);
  108. void SevenToEuc(void);
  109. void JisRepair(FILEFORMAT outcode, char *ki, char *ko);
  110. BOOL SkipESCSeq(int ch, int *TwoBytes);
  111. FILEFORMAT DetectCodeType(void);
  112.  
  113.  
  114.  
  115. /* The I/O Routines */
  116.  
  117. static int (* f_charin)(void);
  118. static void (* f_charout)(int);
  119.  
  120. static int UngetBuf[UNGETBUFSIZ];
  121. static int stackp;
  122.  
  123.  
  124.  
  125.  
  126. void SetupIO (int (* in)(void), void (* out)(int))
  127. {
  128.     f_charin = in;
  129.     f_charout = out;
  130.     stackp = 0;
  131. }
  132.  
  133.  
  134. static void OutPuts (char *buf)
  135. {
  136.     for (; *buf; buf++) WriteChar(*buf);
  137. }
  138.  
  139.  
  140.  
  141. /* Convert input file ==> EUC */
  142.  
  143. void FileImport (FILEFORMAT incode)
  144. {
  145.     switch (incode) {
  146.         case FF_UNKNOWN:    StraightEcho(); break;
  147.  
  148.         case FF_OLDJIS:
  149.         case FF_NEWJIS:
  150.         case FF_NEC:        SevenToEuc(); break;
  151.  
  152.         case FF_EUC:        EucToEuc(incode); break;
  153.  
  154.         case FF_SJIS:       ShiftToEuc(incode); break;
  155.     }
  156. }
  157.  
  158.  
  159. /* Convert internal EUC ==> Output format */
  160.  
  161. void FileExport (FILEFORMAT outcode)
  162. {
  163.     switch (outcode) {
  164.         case FF_NEWJIS:     EucToSeven(FF_EUC, "$B", "(J"); break;
  165.         case FF_OLDJIS:     EucToSeven(FF_EUC, "$@", "(J"); break;
  166.         case FF_NEC:        EucToSeven(FF_EUC, "K", "H"); break;
  167.  
  168.         case FF_UNKNOWN:
  169.         case FF_EUC:        /*EucToEuc(FF_EUC); */ StraightEcho(); break;
  170.  
  171.         case FF_SJIS:       EucToShift(FF_EUC); break;
  172.     }
  173. }
  174.  
  175.  
  176. #ifdef FOOBAR
  177. void main(int argc,char **argv)
  178. {
  179.   *out;
  180. #ifndef THINK_C
  181.   int rc;
  182. #endif
  183.   int tempincode,incode,doing = FALSE,forcedelesc = FALSE;
  184.   int makeoutfile = TRUE,outcode = FF_UNKNOWN,delesc = FALSE;
  185.   int repairjis = FALSE,TOFULLSIZE = FALSE,setincode = FALSE,docheck = FALSE;
  186.   char infilename[100],outfilename[100],extension[10],ki[10],ko[10],toolname[20];
  187.  
  188. #ifdef THINK_C
  189.   argc = ccommand(&argv);
  190. #endif
  191.  
  192.   strcpy(toolname,*argv);
  193.   while (--argc > 0 && (*++argv)[0] == '-')
  194.     switch (ToUpperCase(*++argv[0])) {
  195.       case 'C' :
  196.         docheck = TRUE;
  197.         break;
  198.       case 'F' :
  199.         TOFULLSIZE = TRUE;
  200.         break;
  201.       case 'H' :
  202.         dohelp(toolname);
  203.         break;
  204.       case 'I' :
  205.         setincode = TRUE;
  206.         doing = INPUT;
  207.         incode = getcode(extension,ToUpperCase(*++argv[0]),ki,ko,doing);
  208.         break;
  209.       case 'O' :
  210.         doing = OUTPUT;
  211.         outcode = getcode(extension,ToUpperCase(*++argv[0]),ki,ko,doing);
  212.         break;
  213.       case 'R' :
  214.         repairjis = TRUE;
  215.         doing = REPAIR;
  216.         outcode = getcode(extension,ToUpperCase(*++argv[0]),ki,ko,doing);
  217.         break;
  218.       case 'S' :
  219.         delesc = TRUE;
  220.         strcpy(extension,".rem");
  221.         if (ToUpperCase(*++argv[0]) == 'F')
  222.           forcedelesc = TRUE;
  223.         break;
  224.       case 'T' :
  225.         switch (ToUpperCase(*++argv[0])) {
  226.           case 'E' :
  227.             doeuctable();
  228.             break;
  229.           case 'J' :
  230.           case 'N' :
  231.           case 'O' :
  232.             dojistable();
  233.             break;
  234.           case 'S' :
  235.             dosjistable();
  236.             break;
  237.           default :
  238.             dojistable();
  239.             dosjistable();
  240.             doeuctable();
  241.             break;
  242.         }
  243.         exit(0);
  244.         break;
  245.       case 'V' :
  246.         break;
  247.       default :
  248.         fprintf(stderr,"Illegal option \"-%c\"! Try using the \"-h\" option for help.\n",*argv[0]);
  249.         fprintf(stderr,"Usage: %s [-options] [infile] [outfile]\nExiting...\n",toolname);
  250.         exit(1);
  251.         break;
  252.     }
  253.   if (repairjis && delesc) {
  254.     fprintf(stderr,"Error! Both \"-r\" and \"-s\" options cannot be selected! Exiting...\n");
  255.     exit